//
//  MXGesturesViewController.m
//  MXLoginMethodsDomo
//
//  Created by 马彦 on 16/9/27.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXGesturesViewController.h"
#import "UITableViewCell+Cellutils.h"
#import "MXGesturesCell.h"
#import "MXLockViewController.h"

#import "MXGresturesSwitchModel.h"
#import "MXGresturesArrowModel.h"

#import "MXLockPassword.h"

@interface MXGesturesViewController ()<UITableViewDelegate,UITableViewDataSource,MXGesturesCellDelegate,MXLockViewControlerDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) BOOL isSwitch;

@end

@implementation MXGesturesViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
     [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self dataArray];
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([MXLockPassword oldPassword]) {
        return self.dataArray.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *GesturesCell = @"GesturesCell";
 
        MXGesturesCell *cell = [tableView dequeueReusableCellWithIdentifier:GesturesCell];
        if (!cell) {
            cell = [[MXGesturesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GesturesCell];
        }
        
        cell.delegate = self;
        cell.model = self.dataArray[indexPath.row];
        [cell addBottomHorizontalLine:5];
        
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CellHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (1 == indexPath.row) {
        MXLockViewController *lockVC = [[MXLockViewController alloc] initWithType:MXLockTypeModify appearMode:MXLockAppearModePresent];
        lockVC.delegate = self;
        lockVC.appearMode = MXLockAppearModePush;
        [self.navigationController pushViewController:lockVC animated:YES];
    }
}

#pragma makr -MXGesturesCellDelegate

- (void)gesturesWithSwitch:(UISwitch *)swi {
    self.isSwitch = swi.on;
    if (swi.on) {
        MXLockViewController *lockVC = [[MXLockViewController alloc] initWithType:MXLockTypeCreate appearMode:MXLockAppearModePresent];
        lockVC.delegate = self;
        lockVC.appearMode = MXLockAppearModePush;
        [self.navigationController pushViewController:lockVC animated:YES];
    } else {
        
        MXLockViewController *lockVC = [[MXLockViewController alloc] initWithType:MXLockTypeRemove appearMode:MXLockAppearModePresent];
        lockVC.delegate = self;
        lockVC.appearMode = MXLockAppearModePush;
        [self.navigationController pushViewController:lockVC animated:YES];
    }
}


#pragma makr - MXLockViewControlerDelegate

- (void)passwordDidCreate:(NSString *)newPassword {
    NSLog(@"创建手势密码成功:%@",newPassword);
    
    [self.tableView reloadData];
}

- (void)passwordDidRemove {
    NSLog(@"清除密码成功");
    
    [self.tableView reloadData];
}

- (void)passwordDidModify:(NSString *)newPassword {
    NSLog(@"修改密码成功");
  
}

#pragma mark -lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = RGBA_Color(0.97, 0.97, 0.97, 1.0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


-(NSMutableArray *)dataArray {
    if (!_dataArray) {
        
        _dataArray = [NSMutableArray array];
        MXGresturesSwitchModel *switchModel = [MXGresturesSwitchModel gesturesWithTitle:@"手势密码"];
        MXGresturesArrowModel *iconModle = [MXGresturesArrowModel gesturesWithTitle:@"修改手势密码"];
        [_dataArray addObject:switchModel];
        [_dataArray addObject:iconModle];
    }
    return _dataArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
